﻿Public Class RoundedCornerOperation
    Inherits ImageOperationBase
    Private _radius As Integer
    Private _color As Color
    Private _type As CornerType
    Private _backgroundcolor As Color
    Private _width As Integer
    Private _height As Integer
    Public Sub New(ByVal Radius As Integer, ByVal BackgroundColor As Color, ByVal Color As Color, ByVal Type As CornerType)
        _radius = Radius
        _type = Type
        _color = Color
        _backgroundcolor = BackgroundColor
    End Sub
    Public Overrides Sub ExecuteExtension(ByRef g As System.Drawing.Graphics, ByRef b As System.Drawing.Bitmap)
        _width = b.Width
        _height = b.Height
        If _radius <> 0 Then
            Dim halfradius As Integer = CInt(_radius / 2)
            Dim corner As Point
            Dim startarc As New Point
            Dim endarc As New Point
            Dim startangle As Integer
            Dim x As Integer
            Dim y As Integer
            Select Case _type
                Case CornerType.LeftBottom
                    startangle = 90
                    corner = New Point(0, _height)
                    startarc = New Point(0, _height - halfradius + 1)
                    endarc = New Point(_radius / 2, _height + 1)
                    x = 0
                    y = _height - _radius - 1
                Case CornerType.LeftTop
                    startangle = -180
                    corner = New Point(0, 0)
                    startarc = New Point(0, _radius / 2)
                    endarc = New Point(_radius / 2, 0)
                    x = 0
                    y = 0
                Case CornerType.RightTop
                    startangle = -90
                    corner = New Point(_width, 0)
                    startarc = New Point(_width - halfradius, 0)
                    endarc = New Point(_width, halfradius)
                    x = _width - _radius - 1
                    y = 0
                Case CornerType.RightBottom
                    startangle = 0
                    corner = New Point(_width, _height)
                    startarc = New Point(_width - halfradius - 1, _height - 1)
                    endarc = New Point(_width - 1, _height - halfradius - 1)
                    x = _width - _radius - 1
                    y = _height - _radius - 1
            End Select
            Dim p As New GraphicsPath
            p.AddLine(corner, startarc)
            p.AddArc(x, y, _radius, _radius, startangle, 90)
            p.AddLine(endarc, corner)
            p.Flatten()
            g.SetClip(p)
            g.FillRectangle(New SolidBrush(_backgroundcolor), 0, 0, _width, _height)
            g.ResetClip()
            g.DrawArc(New Pen(_color), x, y, _radius, _radius, startangle, 90)
        End If
    End Sub
End Class